package clientView;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.Contract;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import clientView.Contract_View.Mouse_tblContract;
import database.ContractDelegate;

public class Contract_Insert  extends JDialog implements ActionListener{

	private JTextField txtConName;
	private JLabel lblConName;
	private JLabel lblConDes;
	private JTextArea txaConDes;
	private JScrollPane scrollPane;
	private JButton btnSave;
	private JButton btnCancel;

	public Contract_Insert (JFrame main) {
		super(main, "Thêm Hợp Đồng", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		int height = 200;
	    int width = 450;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		setFont(new Font("Times New Roman", Font.PLAIN, 13));
		setResizable(false);
	    initComponet();
	}
	    
	    public void initComponet()
	    {	
	    GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.5, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblConName = new JLabel("Tên Hợp Đồng     :");
		lblConName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmTName = new GridBagConstraints();
		gbc_lblEmTName.anchor = GridBagConstraints.WEST;
		gbc_lblEmTName.insets = new Insets(15, 15, 5, 5);
		gbc_lblEmTName.fill = GridBagConstraints.VERTICAL;
		gbc_lblEmTName.gridx = 0;
		gbc_lblEmTName.gridy = 0;
		getContentPane().add(lblConName, gbc_lblEmTName);
		
		txtConName = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtConName, gbc_textField);
		txtConName.setColumns(10);
		txtConName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtConName.setText(StringUtil.removeUnwantedSpace(txtConName.getText()));
			}
		});
		//add event de check database
		txtConName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				ContractDelegate d = new ContractDelegate();
				if(!d.CheckInsertContractName(StringUtil.removeUnwantedSpace((txtConName.getText())))&&txtConName.getText().length()>3){
					btnSave.setEnabled(true);
				}else{
					btnSave.setEnabled(false);
					
				}
			
			}
		});
		lblConDes = new JLabel("Mô tả Hợp Đồng :");
		lblConDes.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblConDes = new GridBagConstraints();
		gbc_lblConDes.anchor = GridBagConstraints.WEST;
		gbc_lblConDes.insets = new Insets(5, 15, 5, 5);
		gbc_lblConDes.fill = GridBagConstraints.VERTICAL;
		gbc_lblConDes.gridx = 0;
		gbc_lblConDes.gridy = 1;
		getContentPane().add(lblConDes, gbc_lblConDes);
		
		scrollPane = new JScrollPane();
		txaConDes = new JTextArea();
		txaConDes.setRows(4);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 2;
		gbc_scrollPane.insets = new Insets(5, 5, 5, 15);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 1;
		scrollPane.setViewportView(txaConDes);
		getContentPane().add(scrollPane, gbc_scrollPane);
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
		
	
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave){
			if(!StringUtil.isValidName(txtConName.getText(),200,2)){
				JOptionPane.showMessageDialog(null, "Tên Hợp Đồng Ko để trống", 
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
			}else{
				ContractDelegate c = new ContractDelegate();
				Contract contract = new Contract();
				contract.setContractName(StringUtil.removeUnwantedSpace(txtConName.getText()));
				contract.setContractDes(StringUtil.removeUnwantedSpace(txaConDes.getText()));
				boolean flag = c.insertContract(contract, Main.userId);
				if(flag == true){
					this.setVisible(false);
					JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
							UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
					Contract_View.tblContract.setVisible(false);
					Contract_View.tblContract = UtilityGUI.getTable("Contract", 1, "Contract_Id", true);
					Contract_View.tblContract.addMouseListener(new Mouse_tblContract ());
					Contract_View.scrollPane.setViewportView(Contract_View.tblContract);
					dispose();
				}else{
					JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
							UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					return;
				}
			}
			
		}else if(e.getSource() == btnCancel){
			dispose();
		}
		
	}
}
